export const setLocal = (key: string, value: any) => {
    localStorage.setItem(key, JSON.stringify(value))
}

/**
 * 用于从本地获取数据，如果不存在则返回false
 * @param key
 * @returns
 */
export const getLocal = (key: string) => {
    const jsonValue = localStorage.getItem(key)

    try {
        if (jsonValue === 'undefined') {
            return false
        } else if (jsonValue === null) {
            return false
        } else {
            if (Object.keys(jsonValue).length <= 0) {
                return false
            } else {
                return JSON.parse(jsonValue)
            }
        }
    } catch (error) {
        console.log(error)
        return false
    }
}

/**
 *
 * @param key string[] 数组就代表批量删除
 */
export const removeLocal = (key: string[]) => {
    key.forEach((item: string) => {
        localStorage.removeItem(item)
    })
}

export const clearLocal = () => {
    localStorage.clear()
}
